<?php
/*
 * Aclass_nav 导航类
 * 2010-07-08
 * http://www.abans.cn		abans@qq.com
 */

class Aclass_nav{
	function  __construct() {
	}
	/*
	function nav($navname){
		//检查菜单是否已生成
		$Anav = Afun_cache_get($navname);
		if(empty($Anav)){
			Afun_nav_create($navname);
		}
		return ;
	}*/
	function anav_data($name=""){
		$cachename = 'anavs:'.$name;
		$navdata = Afun_cache_get('Anav');
		$nav = $navdata->$cachename;
		if(empty($nav)){
			$this->navs = Afun_db()->model(Acan_table_navs);
			$con->where = "pid='0'";
			if($name){
				$con->where .= " and url='$name'";
			}
			$con->order = "`look` ASC";
			$models = $this->navs->ofindAll($con);
			if(!empty($name)){
				$models[0]->aurl = $if_start.$models[0]->url;
				$nav = $this->anav_datas($models[0],'',$if_start);
			}else{
				foreach($models as $k=>$v){
					$aurl = $if_start.$v->aurl;
					$v->url = '/';
					$data->$aurl = $this->anav_datas($v->id,$v->url,$if_start);
				}
				$nav = $data;
			}
			$navdata->$cachename = $nav;
			Afun_cache_set('Anav',$navdata,0);//写入缓存
		}
		return $nav;
	}
	//递归获取菜单
	function anav_datas($obj,$app_url="",$if_start=''){
		$this->navs = Afun_db()->model(Acan_table_navs);
		$con->where = "pid='$obj->id'";
		$con->order = "`look` ASC";
		$models = $this->navs->ofindAll($con);
		if($models){
			foreach ($models as $k=>$val){
				if($val->if_show=='1'){
					if($val->if_url=='0' && !empty($app_url)){
						$val->aurl = $if_start.$app_url.'-'.$val->url;
					}elseif($val->if_url=='1'){//外链
						$val->aurl = $val->aurl;
					}else{
						$val->aurl = $if_start.$val->url;
					}
					$val->child = $this->anav_datas($val,$val->url,$if_start);
					$aurl = $val->url;
					$apps->$aurl = $val;
				}
			}
			return $apps;
		}else{
			return false;
		}
	}

	function nav_data($name="",$if_index='0'){
		$this->cachename = 'anav:'.$name;
		$nav = Afun_cache_get($this->cachename);
		if(empty($nav)){
			$con->where = "pid='0'";
			$con->order = "`look` ASC";
			$models = $this->nav->ofindAll($con);
			if(!empty($name)){
				foreach($models as $k=>$v){
					if($v->aurl == $name){
						if($if_index==1){
							$nav = $this->nav_datas($v->id,'/');
						}else{
							$nav = $this->nav_datas($v->id,'/'.$v->aurl.'.php?a=');
						}
					}
				}
			}else{
				foreach($models as $k=>$v){
					$aurl = $v->aurl;
					$v->url = '/'.$aurl.'.php';
					$data->$aurl = $this->nav_datas($v->id,$v->url.'?a=');
				}
				$nav = $data;
			}
			Afun_cache_set($this->cachename,$nav);//写入缓存
		}
		return $nav;
	}
	//递归获取菜单
	function nav_datas($pid,$app_url){
		$con->where = "pid='$pid'";
		$con->order = "`look` ASC";
		$models = $this->nav->ofindAll($con);
		if($models){
			foreach ($models as $k=>$val){
				if($val->if_show=='1'){
					if($val->class>='1'){
						if($val->class=='2')
							$val->url = $app_url.$val->aurl;
						else
							$val->url = $app_url.'/'.$val->aurl;
					}else{//外部菜单
						$val->url = $val->other;
					}
					$val->child = $this->nav_datas($val->id,$val->url);
					$aurl = $val->aurl;
					$apps->$aurl = $val;
				}
			}
			return $apps;
		}else{
			return false;
		}
	}

//--更新菜单(不存在则新加)
	function nav_create(){
		$Aclass_file = Afunc_file();
		$nav_pdir = $Aclass_file->ascandiro(A_ROOT.'/app/module/')->dir;//读取app name
		foreach ($nav_pdir as $p=>$app){
			if($app == '.svn'){ continue; }//屏蔽不正常的文件夹
			$pid0 = $this->nav_create_sql($app,'0','1');//存取app name的ID(不存在则插入,存在则取出ID)
			$nav_dir = $Aclass_file->ascandiro(A_ROOT.'/app/module/'.$app.'/')->dir;//读取app下的module name
			foreach ($nav_dir as $k=>$module){
				if($module == '.svn'){ continue; }//屏蔽不正常的文件夹
				$pid1 = $this->nav_create_sql($module,$pid0,'2');//存取module name的ID(不存在则插入,存在则取出ID)
				$str = $Aclass_file->ascandiro(A_ROOT.'/app/module/'.$app.'/'.$module.'/')->file;//获取module下的act
				foreach ($str as $i=>$act){
					$act = str_replace('.php', '', $act);
					$this->nav_create_sql($act,$pid1,'3');//存取act name(不存在则插入,存在则取出ID)
				}
			}
		}
		return $nav_dir;
	}

	/*
	 * 创建菜单
	 * 新纪录则返回 最后插入的ID
	 * 存在纪录则返回 自己的ID
	 */
	function nav_create_sql($aurl,$pid,$class){
		$navcount = Afun_db()->query("SELECT count(*) as count,id FROM `".Acan_table_nav."` WHERE `pid`='$pid' and `class`='$class' and `aurl`='$aurl'")->fetch();
		if($navcount['count']=='0'){
			Afun_db()->exec("INSERT INTO `".Acan_table_nav."` (`pid`,`aurl`,`class`,`name`) VALUES ('$pid','$aurl','$class','$aurl')");
			return Afun_db()->lastInsertId();
		}
		return $navcount['id'];
	}


}

?>
